<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/4/13
 * Time: 上午11:11
 *
 * 功能：系统定时维护过期的优惠券配置，标识状态为过期状态
 * 版本：v1.0
 *
 *
 * 规则：每隔一分钟执行一次
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\activity\OutCouponConfig as mOutCouponConfig;
use Biz\service\activity\OutCoupon as sOutCoupon;
use Biz\module\Classes\OutCoupon as clsOutCoupon;

if (isset($argv[1])) {
    $stopMiunte = date('Y-m-d H:i:00', strtotime($argv[1]));
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}

$startMiunte = date('Y-m-d H:i:00', strtotime('-1 Minute'));


$where = [
    'stop_time[<>]' => [$startMiunte, $stopMiunte],
    'status'        => mOutCouponConfig::STATUS_ONLINE,
];

try {
    //取出待处理的数据
    $outcouponList = mOutCouponConfig::select("*", $where);

    if (empty($outcouponList)) {
        throw new \Exception("当前时刻没有待处理的数据");
    }

    foreach ($outcouponList as $outcouponConfig) {

        try {

            clsOutCoupon::setOffline($outcouponConfig['id']);


        } catch (\Exception $e) {

            echo sprintf("%s    时刻:%s   outcoupon_id:%s   异常：%s  \n", date('Y-m-d H:i:s'), $stopMiunte, $outcouponConfig['id'], json_encode([
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine(),
                'e_msg'  => $e->getMessage()
            ], JSON_UNESCAPED_UNICODE));

        } finally {
            echo sprintf("%s    时刻:%s    outcoupon_id:%s   sql日志：%s   \n", date('Y-m-d H:i:s'), $stopMiunte, $outcouponConfig['id'], mOutCouponConfig::sqlLogs());

            mOutCouponConfig::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    %s 时刻:%s    sql日志：%s\n", date('Y-m-d H:i:s'), $e->getMessage(), $stopMiunte, mOutCouponConfig::sqlLogs());

    mOutCouponConfig::clearLogs();


}






